{% extends 'blog/base.html' %}

{% block title %}{{ title }} - 个人博客{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header">
                <h4><i class="fas fa-edit"></i> {{ title }}</h4>
            </div>
            <div class="card-body">
                <form method="post">
                    {% csrf_token %}
                    <div class="mb-3">
                        <label for="{{ form.title.id_for_label }}" class="form-label">{{ form.title.label }}</label>
                        {{ form.title }}
                        {% if form.title.errors %}
                            <div class="text-danger">
                                {% for error in form.title.errors %}
                                    <small>{{ error }}</small>
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>

                    <div class="mb-3">
                        <label for="{{ form.slug.id_for_label }}" class="form-label">{{ form.slug.label }}</label>
                        {{ form.slug }}
                        <div class="form-text">URL别名，只能包含字母、数字、连字符和下划线</div>
                        {% if form.slug.errors %}
                            <div class="text-danger">
                                {% for error in form.slug.errors %}
                                    <small>{{ error }}</small>
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>

                    <div class="mb-3">
                        <label for="{{ form.category.id_for_label }}" class="form-label">{{ form.category.label }}</label>
                        {{ form.category }}
                        {% if form.category.errors %}
                            <div class="text-danger">
                                {% for error in form.category.errors %}
                                    <small>{{ error }}</small>
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>

                    <div class="mb-3">
                        <label for="{{ form.excerpt.id_for_label }}" class="form-label">{{ form.excerpt.label }}</label>
                        {{ form.excerpt }}
                        <div class="form-text">文章摘要，用于在列表中显示（可选）</div>
                        {% if form.excerpt.errors %}
                            <div class="text-danger">
                                {% for error in form.excerpt.errors %}
                                    <small>{{ error }}</small>
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>

                    <div class="mb-3">
                        <label for="{{ form.content.id_for_label }}" class="form-label">{{ form.content.label }}</label>
                        {{ form.content }}
                        {% if form.content.errors %}
                            <div class="text-danger">
                                {% for error in form.content.errors %}
                                    <small>{{ error }}</small>
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>

                    <div class="mb-3">
                        <label for="{{ form.status.id_for_label }}" class="form-label">{{ form.status.label }}</label>
                        {{ form.status }}
                        <div class="form-text">选择"草稿"保存但不发布，选择"已发布"立即发布</div>
                        {% if form.status.errors %}
                            <div class="text-danger">
                                {% for error in form.status.errors %}
                                    <small>{{ error }}</small>
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>

                    <div class="d-flex justify-content-between">
                        <a href="{% if post %}{{ post.get_absolute_url }}{% else %}{% url 'blog:post_list' %}{% endif %}" 
                           class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> 返回
                        </a>
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save"></i> 保存文章
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
// 自动生成slug
document.getElementById('{{ form.title.id_for_label }}').addEventListener('input', function() {
    const title = this.value;
    const slug = title
        .toLowerCase()
        .replace(/[^\w\s-]/g, '') // 移除特殊字符
        .replace(/\s+/g, '-')     // 空格替换为连字符
        .replace(/-+/g, '-')      // 多个连字符替换为单个
        .trim();
    document.getElementById('{{ form.slug.id_for_label }}').value = slug;
});
</script>
{% endblock %}
